1 package edu.jiangxin.apktoolbox.file;
2
3 import edu.jiangxin.apktoolbox.file.core.EncoderConvert;
4 import edu.jiangxin.apktoolbox.file.core.EncoderDetector;
5 import edu.jiangxin.apktoolbox.swing.extend.autocomplete.AutoCompleteComboBox;
6 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
7 import edu.jiangxin.apktoolbox.swing.extend.FileListPanel;
8 import edu.jiangxin.apktoolbox.utils.Constants;
9 import org.apache.commons.io.FileUtils;
10 import org.apache.commons.lang3.StringUtils;
11
12 import javax.swing.*;
13 import java.awt.*;
14 import java.awt.event.*;
15 import java.io.File;
16 import java.io.IOException;
17 import java.nio.charset.Charset;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23
24
25
26
27
28 public class EncodeConvertPanel extends EasyPanel {
29 private static final long serialVersionUID = 1L;
30
31 private FileListPanel srcPanel;
32
33 private JPanel optionPanel;
34
35 private JTextField suffixTextField;
36
37 private JCheckBox autoDetectCheckBox;
38
39 private AutoCompleteComboBox<String> fromComboBox;
40
41 private JCheckBox recursiveCheckBox;
42
43 private AutoCompleteComboBox<String> toComboBox;
44
45 private JPanel operationPanel;
46
47 public EncodeConvertPanel() throws HeadlessException {
48 super();
49 }
50
51 @Override
52 public void initUI() {
53 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
54
55 createSrcPanel();
56 add(srcPanel);
57 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
58
59 createOptionPanel();
60 add(optionPanel);
61 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
62
63 createOperationPanel();
64 add(operationPanel);
65 }
66
67 private void createSrcPanel() {
68 srcPanel = new FileListPanel();
69 }
70
71 private void createOptionPanel() {
72 optionPanel = new JPanel();
73 optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
74
75 JLabel suffixLabel = new JLabel("Suffix:");
76 optionPanel.add(suffixLabel);
77 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
78
79 suffixTextField = new JTextField();
80 suffixTextField.setToolTipText("an array of extensions, ex. {\"java\",\"xml\"}. If this parameter is empty, all files are returned.");
81 suffixTextField.setText(conf.getString("encodeconvert.suffix"));
82 optionPanel.add(suffixTextField);
83 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
84
85 recursiveCheckBox = new JCheckBox("Recursive");
86 recursiveCheckBox.setSelected(true);
87 optionPanel.add(recursiveCheckBox);
88 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
89
90 JLabel fromLabel = new JLabel("From:");
91 optionPanel.add(fromLabel);
92 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
93
94 fromComboBox = new AutoCompleteComboBox<String>();
95 fromComboBox.setEnabled(false);
96 optionPanel.add(fromComboBox);
97 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
98
99 autoDetectCheckBox = new JCheckBox("Auto Detect");
100 autoDetectCheckBox.setSelected(true);
101 autoDetectCheckBox.addItemListener(e -> fromComboBox.setEnabled(!(e.getStateChange() == ItemEvent.SELECTED)));
102 optionPanel.add(autoDetectCheckBox);
103 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
104
105 JLabel toLabel = new JLabel("To:");
106 optionPanel.add(toLabel);
107 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
108
109 toComboBox = new AutoCompleteComboBox<String>();
110 toComboBox.setSelectedItem(conf.getString("encodeconvert.to"));
111 optionPanel.add(toComboBox);
112
113 for (String charset : Charset.availableCharsets().keySet()) {
114 fromComboBox.addItem(charset);
115 toComboBox.addItem(charset);
116 }
117 }
118
119 private void createOperationPanel() {
120 operationPanel = new JPanel();
121 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
122 JButton convertButton = new JButton("Convert");
123 convertButton.addActionListener(new ConvertButtonActionListener());
124
125 operationPanel.add(convertButton);
126 }
127
128 private class ConvertButtonActionListener implements ActionListener {
129 @Override
130 public void actionPerformed(ActionEvent e) {
131 conf.setProperty("encodeconvert.suffix", suffixTextField.getText());
132 conf.setProperty("encodeconvert.to", toComboBox.getSelectedItem().toString());
133
134 try {
135 List<File> fileList = new ArrayList<>();
136 for (File file : srcPanel.getFileList()) {
137 String[] extensions = null;
138 if (StringUtils.isNotEmpty(suffixTextField.getText())) {
139 extensions = suffixTextField.getText().split(",");
140 }
141 fileList.addAll(FileUtils.listFiles(file, extensions, recursiveCheckBox.isSelected()));
142 }
143 Set<File> fileSet = new TreeSet<>(fileList);
144 fileList.clear();
145 fileList.addAll(fileSet);
146
147 for (File file : fileList) {
148 String fromEncoder;
149 if (autoDetectCheckBox.isSelected()) {
150 fromEncoder = EncoderDetector.judgeFile(file.getCanonicalPath());
151 } else {
152 fromEncoder = fromComboBox.getSelectedItem().toString();
153 }
154 String toEncoder = toComboBox.getSelectedItem().toString();
155 logger.info("processing: {} from {} to {}", file.getCanonicalPath(), fromEncoder, toEncoder);
156 EncoderConvert.encodeFile(file.getCanonicalPath(), fromEncoder, toEncoder);
157 logger.info("processed");
158 }
159
160 logger.info("convert finish");
161 } catch (IOException e1) {
162 logger.error("convert fail", e1);
163 }
164 }
165 }
166 }